<template>
  <!--随手拍-->
  <div class="mod-bus__busmoment">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.creator" placeholder="发布人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="state.dataForm.isOpen" clearable placeholder="是否公开">
          <el-option v-for="item in isOpenOptions" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="state.dataForm.replyStatus" clearable placeholder="回复状态">
          <el-option v-for="item in replyStatusOptions" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('bus:busmoment:save')" type="primary" @click="addOrUpdateHandle()">
          {{ $t("add") }}
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('bus:busmoment:delete')" type="danger" @click="state.deleteHandle()">
          {{ $t("deleteBatch") }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
              @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <div style="margin-left: 50px">
            <el-row>
              <el-col :span="24">随手拍内容: {{ props.row.content }}</el-col>
            </el-row>
            <el-row v-if="props.row.photo">
              <el-col :span="24">
                <el-image v-for="(item, index) in props.row.photo.split(',')" :key="index + '_image'"
                          style="width: 100px; height: 100px; margin-right: 10px" :src="'https://' + item"
                          fit="cover"></el-image>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">回复内容: {{ props.row.replyContent }}</el-col>
            </el-row>
            <el-row>
              <el-col :span="24">回复时间: {{ props.row.replyTime }}</el-col>
            </el-row>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="content" label="内容" header-align="center" align="center"></el-table-column>
      <el-table-column prop="creatorName" label="发布人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="提交时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="replyContent" label="回复内容" header-align="center" align="center"></el-table-column>
      <el-table-column prop="replyUser" label="回复人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="replyTime" label="是否回复" header-align="center" align="center">
        <template v-slot="scope">
          {{ scope.row.replyTime ? "已回复" : "未回复" }}
        </template>
      </el-table-column>
      <el-table-column prop="isOpen" label="是否公开" header-align="center" align="center">
        <template v-slot="scope">
          <el-switch :model-value="scope.row.isOpen" :active-value="1" :inactive-value="0"
                     @click="publishRow(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="200">
        <template v-slot="scope">
          <!--          <el-button v-if="state.hasPermission('bus:busmoment:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>-->
          <el-button v-if="state.hasPermission('bus:busmoment:update')" type="primary" link
                     @click="replyHandle(scope.row.id)">{{ $t("reply") }}
          </el-button>
          <el-button v-if="state.hasPermission('bus:busmoment:delete')" type="primary" link
                     @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey+1" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
    <!-- 弹窗, 回复 -->
    <reply :key="addKey" ref="replyRef" @refreshDataList="state.getDataList"></reply>
  </div>
</template>

<style scoped>
.el-row {
  margin-bottom: 10px;
}
</style>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {IObject} from "@/types/interface";
import baseService from "@/service/baseService";
import {nextTick, reactive, ref, toRefs} from "vue";
import AddOrUpdate from "./walkabout-view-add.vue";
import Reply from "./busmoment-reply.vue";

const view = reactive({
  getDataListURL: "/bus/busmoment/page",
  getDataListIsPage: true,
  exportURL: "/bus/busmoment/export",
  deleteURL: "/bus/busmoment",
  deleteIsBatch: true,
  dataForm: {
    creator: "",
    isOpen: "",
    replyStatus: "",
    type: "ssp"
  }
});

const replyStatusOptions = [
  {
    value: "0",
    label: "未回复"
  },
  {
    value: "1",
    label: "已回复"
  }
];

const isOpenOptions = [
  {
    value: "0",
    label: "不公开"
  },
  {
    value: "1",
    label: "公开"
  }
];

const state = reactive({...useView(view), ...toRefs(view)});

const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
const replyRef = ref();
const replyHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    replyRef.value.init(id);
  });
};
// 是否发布
const publishRow = (row: IObject) => {
  row.isOpen = row.isOpen ? 0 : 1;
  baseService.put("/bus/busmoment/", row).then((res) => {
    state.getDataList();
  });
};
</script>
